//
//  JYFunView.m
//  JYFarm
//
//  Created by 花花 on 2017/10/13.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYFunView.h"

#import "JYCategorysModel.h"
@interface JYFunView()<UICollectionViewDelegate,UICollectionViewDataSource>

@end
@implementation JYFunView

static NSString *const JYFunCellID = @"JYFunCellID"; //功能cellID
- (instancetype)initWithFrame:(CGRect)frame{


    if (self = [super initWithFrame:frame]) {
  
        
        [self setupSubView];
    }
    return self;

}

-(void)setDataArr:(NSMutableArray *)dataArr{

    _dataArr = dataArr;
    
    [self collectionView:_collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];

    [self.collectionView reloadData];

}


- (void)setupSubView{

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, self.height) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    
    _collectionView.alwaysBounceVertical = YES;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_collectionView];
    
    [_collectionView registerClass:[JYFunCell class]  forCellWithReuseIdentifier:JYFunCellID];


}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{


    return CGSizeMake(Screen_Width/5, 78);
}
//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
return UIEdgeInsetsMake(0, 0, 0, 0);

}

//设置每个item的水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{

    return 0;
}

//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
  
    return 0;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    JYFunCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:JYFunCellID forIndexPath:indexPath];
    cell.backgroundColor =[UIColor whiteColor];
    cell.model = [self.dataArr safeObjectAtIndex:indexPath.row];

    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    for (JYCategorysModel *model in self.dataArr) {
        
        model.selectedFlag = NO;
    }
    if (self.dataArr.count <= 0) return;
    JYCategorysModel *catgoryModel = self.dataArr[indexPath.row];
    catgoryModel.selectedFlag = YES;

    
    JYCategorysModel *model = [self.dataArr safeObjectAtIndex:indexPath.row];
    
    if (_FunDidSelectItemAtIndexPath) {
        _FunDidSelectItemAtIndexPath(model.childrenListArr);
    }
    
    [collectionView reloadData];
   
    NSLog(@"%zd",indexPath.row);

}


@end

@interface JYFunCell()

@end


@implementation JYFunCell

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
//        self.backgroundColor = [UIColor orangeColor];
        [self setupSubView];
        
    }
    return self;

}


//单元格是选中状态
- (void)selectedCell{

    self.tileLabel.textColor = kThemeColor;
}

//单元格不是选中状态
- (void)unselectedCell{
    
    
    self.tileLabel.textColor = k3Color;
}

- (void)layoutSubviews{

    [super layoutSubviews];
    
    JYWeakSelf;
    [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top).offset(10);
        make.centerX.equalTo(weakSelf.mas_centerX);
//        make.width.height.mas_equalTo(60);

        
    }];
    
    
    [_tileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.imgView.mas_bottom).offset(10);
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(-10);
        
        
    }];
    

}

- (void)setupSubView{
    [self.contentView addSubview:self.imgView];
    [self.contentView addSubview:self.tileLabel];

}

- (UIImageView *)imgView{

    if (!_imgView) {
        _imgView = [[UIImageView alloc]init];
//        _imgView.backgroundColor =[UIColor redColor];
    }
    return _imgView;
}

- (UILabel *)tileLabel{


    if (!_tileLabel) {
        _tileLabel = [UILabel labelText:@"" fontSize:13 textColor:k6Color textAlignment:NSTextAlignmentCenter];

    }
    return _tileLabel;
}
- (void)setModel:(JYCategorysModel *)model{


    _model = model;
    if ([model isKindOfClass:[JYCategorysModel class]]) {
       _tileLabel.text = model.name;
        [_imgView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"home_icon_meat"]];
    }
    if (model.selectedFlag) {
        [self selectedCell];
    }else{
        
        [self unselectedCell];
        
    }
}
@end
